package com.lesson8.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class FileController {
	
	
	FileInputStream fin = null;
	

	@RequestMapping("/file/openFile")
	public Map<String, Object> openFile(){
		
		try {
			fin = new FileInputStream(new File("D:/a.txt"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		 Map<String, Object> results = new HashMap<String, Object>();
		 results.put("code", "000000");
		 results.put("msg", "success");
		 return results;
	}
	
	
	@RequestMapping("/file/getFileContent")
	public Map<String, Object> getFileContent(){
		
		 byte[] buffer = new byte[10];
		 ByteArrayOutputStream bout = new ByteArrayOutputStream();
		 int len = -1;
		 if(fin!=null){
//			 try {
//				while((len=fin.read(buffer))!=-1){
//					 bout.write(buffer, 0, len);
//				 }
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
			try {
				fin.read(buffer);
				bout.write(buffer, 0, buffer.length);
			} catch (IOException e) {
			}
			 
		 }else{
			 System.out.println("fin is null");
		 }
		 String s = new String(bout.toByteArray());
		 System.out.println(s);
		 Map<String, Object> results = new HashMap<String, Object>();
		 results.put("code", "000000");
		 results.put("msg", "success");
		 results.put("body", s);
		 return results;
	}

	@RequestMapping("/file/closeFile")
	public Map<String, Object> closeFile(){
		if(fin!=null){
			try {
				fin.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		 Map<String, Object> results = new HashMap<String, Object>();
		 results.put("code", "000000");
		 results.put("msg", "success");
		 return results;
	}
	
	@RequestMapping("/file/upload")
	public Map<String, Object> upload(@RequestParam("file") MultipartFile file){
		InputStream in = null;
		FileOutputStream out = null;
		try {
			in = file.getInputStream();
			String fileName = file.getOriginalFilename();
			 out = new FileOutputStream(new File("d:/"+fileName));
			 int len = -1;
			 byte[] buffer = new byte[1024];
			while((len = in.read(buffer))!=-1){
				out.write(buffer, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
				}
			}
			
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
				}
			}
		}
		
		 Map<String, Object> results = new HashMap<String, Object>();
		 results.put("code", "000000");
		 results.put("msg", "success");
		 return results;
	}
	
}
